class AccountSignin < ActiveRecord::Base
  belongs_to :account, :counter_cache => :account_signin_counter
  belongs_to :terminal

  scope :by_terminal_group, lambda {|terminal_group| joins(:terminal => {:terminal_group => {}}).where(["terminals.terminal_group_id = ?", terminal_group])}
  scope :before_date, lambda {|date| where(["account_signins.created_at < ?", 1.days.since(Time.zone.parse(date)).strftime("%Y-%m-%d")])}
  scope :after_date, lambda {|date| where(["account_signins.created_at >= ?", date])}

  after_create :update_firsted_signin  

  def self.create_from_params account, terminal
    signin = self.new
    signin.account = account
    signin.terminal = terminal
    signin.save
  end

  def update_firsted_signin
    unless self.account.firsted_signin
      account.update_column(:firsted_signin, self.created_at)
      account.update_column(:register_terminal_id, self.terminal_id)

      # Recommand
      if !account.is_recommander && terminal.recommander_id && terminal.recommander_id != account_id
        Recommand.create(
          terminal_id: terminal_id, 
          recommander_id: terminal.recommander_id,
          user_id: account_id
        )
      end
    end
    if account.is_recommander
      terminal.recommander_id = account_id
      terminal.save!
    end
  end

  def self.total_grouped_by(date_part)
    case date_part
    when 'day'
      account_signins = self.group("EXTRACT(YEAR from account_signins.created_at),DATE(account_signins.created_at),account_signins.terminal_id")
      account_signins = account_signins.order("max(EXTRACT(YEAR from account_signins.created_at)) desc,max(DATE(account_signins.created_at)) desc,max(account_signins.terminal_id)")
      account_signins = account_signins.select("EXTRACT(YEAR from account_signins.created_at) as created_year,DATE(account_signins.created_at) as created_part, account_signins.terminal_id as terminal_id, count(account_signins.id) as total")
    when 'week'
      account_signins = self.group("EXTRACT(ISOYEAR from account_signins.created_at),EXTRACT(#{date_part} from account_signins.created_at),account_signins.terminal_id")
      account_signins = account_signins.order("max(EXTRACT(ISOYEAR from account_signins.created_at)) desc,max(EXTRACT(#{date_part} from account_signins.created_at)) desc,max(account_signins.terminal_id)")
      account_signins = account_signins.select("EXTRACT(ISOYEAR from account_signins.created_at) as created_year,EXTRACT(#{date_part} from account_signins.created_at) as created_part, account_signins.terminal_id as terminal_id, count(account_signins.id) as total")
    when 'month','year'
      account_signins = self.group("EXTRACT(YEAR from account_signins.created_at),EXTRACT(#{date_part} from account_signins.created_at),account_signins.terminal_id")
      account_signins = account_signins.order("max(EXTRACT(YEAR from account_signins.created_at)) desc,max(EXTRACT(#{date_part} from account_signins.created_at)) desc,max(account_signins.terminal_id)")
      account_signins = account_signins.select("EXTRACT(YEAR from account_signins.created_at) as created_year,EXTRACT(#{date_part} from account_signins.created_at) as created_part, account_signins.terminal_id as terminal_id, count(account_signins.id) as total")
    end
  end 

  def group_name
    self.terminal.try(:terminal_group).try(:group_name)
  end 

end